package com.neusoft.training.podcast.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.neusoft.training.podcast.entity.User;
import com.neusoft.training.podcast.service.UserService;
@Controller

public class UserController {
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("/userList")
	public ModelAndView users(ModelAndView mv) {
		List<User> users = userService.findAll();
		mv.addObject("users", users);
		mv.setViewName("userMg");
		return mv;
	}
	
	@RequestMapping("/userList1")
	public @ResponseBody List<User> listUsers(){
		
		List<User> users = userService.findAll();
		return users;
		
	}
	
	@RequestMapping("/addUser")
	public String addUser() {
		return "addUser";
	}

	@RequestMapping("/save")
	public String save(User user) {
		userService.insert(user);
		return "redirect:/userList";
	}
	
	@RequestMapping("/searchUser")
	public String searchUser(){
		return "searchUser";
	}
	
	@RequestMapping("/searchUserResult")
	public List<User> result(String name)
	{
		List<User> resultUser = userService.findUserByName(name);
		return resultUser;
	}
	
	@RequestMapping("/updateUserResult")
	public void update(String originalName,String updateName,String updatePwd){
		userService.update(originalName, updateName, updatePwd);
	}
	
	@RequestMapping("/deleteUser")
	public @ResponseBody String delete(String name){
		userService.deleteUser(name);
		return "delete_success";
	}
	
	@RequestMapping("/findUserByID")
	public @ResponseBody User findUserByID(Integer userID){
		User user = userService.findUserByID(userID);
		return user;
	}
	
	@RequestMapping("/login")
	public String login(User user,ModelAndView mv) {
		String loginName=user.getName();
		String loginPwd = user.getPwd();
		User loginUser = userService.login(loginName,loginPwd);

		if (loginUser!=null) {
			mv.addObject("user", loginUser);
			mv.setViewName("index");
			return "login_success";
		}
			
		else return "login_fail";	
	}
	
	@RequestMapping("/register")
	public String register(User user) {
		userService.register(user);
		return 	"redirect:/showVideoList";
	}
	@RequestMapping("/checkEmailExist")
	public @ResponseBody String checkEmialExist(String email) {
		User u = userService.checkEmailExist(email);
		if(u == null) {
			return "false";
		}else {
			return "true";
		}
	}
	@RequestMapping("/checkNameExist")
	public @ResponseBody String checkNameExist(String name) {
		User u = userService.checkNameExist(name);

		if(u == null) {
			return "false";
		}
		return "true";
		
	}
	
	@RequestMapping(value="/doLogin",method=RequestMethod.GET)
	public String doLogin(String email,String pwd,HttpSession session) {

		System.out.println(email);
		User loginUser = userService.login(email,pwd);
		String message = "";
		if (loginUser!=null) {
			session.setAttribute("user", loginUser); 
			 User user=(User)session.getAttribute("user");
			 
		if(loginUser.getIsAdmin()==1)
		{
			return "redirect:/videoList";
		}else {
			return "redirect:/showVideoList";
		}}else {
			message = "登陆失败";
			return message;
		}
	}

	
	@RequestMapping("/loginuser")
	public String login(/*String name,String pwd,HttpSession session*/) {
		return "redirect:/showVideoList";
		//return "redirect:/index2.html";
	}

	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		 
		 session.removeAttribute("user");
		 session.setAttribute("user", null);
		 session.invalidate();
		 
		return "redirect:/showVideoList";
		
	}
	
	@RequestMapping("/uploadHeader")
	public  @ResponseBody Map<String,String> uploadHeader(@RequestParam("header") MultipartFile multipartFile, HttpServletRequest request,String userID) {
		Map<String, String> result = new HashMap<>();
		System.out.println(userID);
		if(multipartFile.isEmpty()) {
			result.put("message", "请勿上传空文件");
			return result;
		}
		String suffix = multipartFile.getOriginalFilename().substring(
				multipartFile.getOriginalFilename().indexOf('.'));
		Random random = new Random();
		String newName = "" + random.nextInt(10000) + System.currentTimeMillis() +  suffix;
		String path = "/podcast/header/";
		String header = path  + newName;
		File destFile = new File(header);
		try {
			multipartFile.transferTo(destFile);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		userService.uploadHeader(header, Integer.parseInt(userID));
		result.put("message", "修改成功");
		result.put("srcPath", header);
		return result;
		
		
	}


	

	
	
	
	
}
